//
//  MCFitnessVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/5.
//

import UIKit
import SnapKit
import JXSegmentedView
import SwiftyJSON
import Toast_Swift
import QMUIKit
import MJRefresh
import Kingfisher

class MCFitnessVc: MCBaseVc {

    let fitnessIdentifier:String = "fitnessIdentifier"
    
    
    var fitnessCollectionV:UICollectionView?
        
    var count = 1
    
    private var dataSource:Array = [MCRecordsAction?]()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        let fitnessCollLayout = UICollectionViewFlowLayout.init()
        
        fitnessCollectionV = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: fitnessCollLayout)
        fitnessCollectionV?.delegate = self
        fitnessCollectionV?.dataSource = self
        fitnessCollectionV?.backgroundColor = UIColor.white
        fitnessCollectionV?.register(MCFitnessCollectionCell.self, forCellWithReuseIdentifier: fitnessIdentifier)
        view.addSubview(fitnessCollectionV!)
        fitnessCollectionV?.snp.makeConstraints { make in
            make.edges.equalTo(view).inset(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
        }
        
        self.fitnessCollectionV?.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.count = 1
            self.getFitnessListData(current: self.count)
        })
        
        self.fitnessCollectionV?.mj_header?.beginRefreshing()
        
        self.fitnessCollectionV?.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            self.getFitnessListData(current: self.count)
        })
        
        
        
    }
    
    
    //网络请求
    func getFitnessListData(current:Int?) -> Void {
        NetWorkRequest(MCMyCollectApi.fitnessList(current: current)) { ResponseModel in
            self.fitnessCollectionV?.mj_footer?.endRefreshing()
            self.fitnessCollectionV?.mj_header?.endRefreshing()
            
            let collectionActionList = MCMyCollectionActionList(JSONString:ResponseModel.dataString)
            
            
            if current == 1 {
                self.dataSource.removeAll()
                self.dataSource.append(contentsOf: collectionActionList?.recordsActions ?? [])
            }else{
                self.count += 1
                self.dataSource.append(contentsOf: collectionActionList?.recordsActions ?? [])
            }
            
            self.fitnessCollectionV?.reloadData()
            
        } failureCallback: { ResponseModel in
            self.fitnessCollectionV?.mj_footer?.endRefreshing()
            self.fitnessCollectionV?.mj_header?.endRefreshing()
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
}

extension MCFitnessVc:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (UIScreen.main.bounds.size.width - 30) / 2, height: 140)
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCFitnessCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: fitnessIdentifier, for: indexPath) as! MCFitnessCollectionCell
        
        let recordActionList = self.dataSource[indexPath.row]
        
        cell.titleInfoLabel.text = recordActionList?.zhName ?? ""
        
        cell.icHeaderImageView.kf.setImage(with: URL(string: recordActionList?.previewUrl ?? ""))
        
        return cell
    }
    
    
}

extension MCFitnessVc:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
